var productDetails = {
    zan: $('.zan'),
    zanNum: $('.zan-num'),
    numMax: 20,
    numMin: 1,
    cartE: $('button'),
    init: function () {
        this.createZan(20);
    },
    createZan: function (num) {
        var n = Math.floor(Math.random() * ((this.numMax - this.numMin + 1) + this.numMin));
        this.zanNum.text(n);
    },
    clickLike: function (id) {
        var t = $('.zan');
        var tm = $('.zan-num');
        if (t.find('img').attr('src').indexOf('love-active') > 0) {
            app.ajax(
                '/Goods/Index/clear_zan',
                {
                    skuid: id
                }, function (res) {
                    if (res.code == 200) {
                        t.find('img').attr('src', '../img/product/love.png');
                        tm.text(res.thing_num);
                    } else {
                        layer.msg('系统繁忙，请稍候再试');
                    }
                });
        } else {
            app.ajax(
                '/Goods/Index/zan',
                {
                    skuid: id
                }, function (res) {
                    console.log(JSON.stringify(res));
                    if (res.code == 200) {
                        t.find('img').attr('src', '../img/product/love-active.png');
                        tm.text(res.thing_num);
                    } else {
                        layer.msg('系统繁忙，请稍候再试');
                    }
                });

        }
    },
    clickCart: function () {
        var id = $('.detail-title').attr('data-id'),
            num = 1,
            warehouseType = $('.detail-title').attr('data-warehouseType'),
            supplierId = $('.detail-title').attr('data-supplierId');
        app.ajax(
            '/Goods/Cart/add_cart',
            {
                skuid: id,
                buyPoductNum: num,
                warehouseType: warehouseType,
                supplierId: supplierId
            }, function (res) {
                if (res.code == 200) {
                    $('footer .index-cart i').addClass("dtl-inum-scale");
                    setTimeout(function () {
                        $('footer .index-cart i').removeClass("dtl-inum-scale");
                    }, 500);
                    /*$('footer .index-cart i').animate({
                     width:20,
                     height:20,
                     right:-5,
                     top:7,
                     lineHeight:'20px'
                     },500);

                     $('footer .index-cart i').animate({
                     width:17,
                     height:17,
                     right:-7,
                     top:10,
                     lineHeight:'17px'
                     },500);*/
                    $('footer .index-cart i').delay(1000).text(res.num);
                    var stock = parseInt($('.detail-title').attr('data-stock'));
                    stock--;
                    $('.detail-title').attr('data-stock', stock);
                }
            });

    }
};

var data = window.sessionStorage.getItem('productDetail');
data = JSON.parse(data).id;
app.ajax(
    '/Goods/Index/goods_view',
    {
        skuid: data
    },
    function (res) {
        if (res.code == 200) {
            app.tpl($('.detail'), 'tpl-detail', res);
            !res.info.video && $('.play').hide();
            var imgs = res.info.detail;
            if (imgs) {
                imgs = imgs.split('/>');
                console.log('第一张图片：' + imgs[0]);
                $('.detail-img.n-first').html(imgs[0] + '/>');
                var html1 = '';
                for (var i = 1; i < imgs.length; i++) {
                    if (i == imgs.length - 1) {
                        html1 += imgs[i];
                    } else {
                        html1 += imgs[i] + '/>';
                    }

                }
                $('.detail-img.n-all').html(html1);
            }
            $('.roundLike').text(parseInt($('.zan-num').text()) + parseInt($('.info-sale span').text()));
        } else {
            layer.msg('系统繁忙，请稍候再试');
        }
    }
);
var is_ms = window.sessionStorage.getItem('productDetail');
is_ms = JSON.parse(is_ms).title;
if (is_ms != 'ms') {
    footerD.init();
}
productDetails.init();

window.onload = function () {

    setTimeout(function () {
        var myswiper1 = new Swiper('.swiper-container', {
            autoplay: 3000,
            pagination: '.swiper-pagination',
            paginationClickable: true,
            autoplayDisableOnInteraction: false,
            loop: true
        });
    }, 1000);


    app.toHistory(window.location.href);

    $('body').on('click', '.zan', function () {
        productDetails.clickLike(data);
    });

    $('body').on('click', '.addCart', function () {
        var stock = parseInt($('.detail-title').attr('data-stock'));
        if (stock > 1) {
            productDetails.clickCart();
        } else {
            alert('库存不足');
        }
    });
    $('body').on('click', '.add-more', function () {
        $('.detail-img.n-all').show();
        $(this).remove();
    });
    //$('body').on('click', '.share.pL15 img', function () {
    $.ajax({
        url: app.link.apiUrl + "/Goods/Cart/getProductEwm",
        type: "get",
        data: {
            skuid: data
        },
        success: function (res) {
            setShare({
                bdText: "自定义Tesc",
                bdDesc: "自定义Desc",
                bdPic: res.result.userpath,
                bdUrl: "http://www.qq.com"
            });
        }
    });
    //});

    /**去结算*/
    $('body').on('click', 'footer .toBuy', function () {
        var id = $('.pSR .detail-title').attr('data-id'),
            stock = parseInt($('.detail-title').attr('data-stock')),
            data = {},
            warehouseType = $('.pSR .detail-title').attr('data-warehouseType'),
            supplierId = $('.pSR .detail-title').attr('data-supplierId');
        data = {
            supplier_id: supplierId,
            warehouse_type: warehouseType,
            skuid: id
        };
        console.log(data);
        var id = $('.detail-title').attr('data-id');
        if (stock >= 1) {
            console.log(1);
            footerD.buy(data);
        } else {
            layer.msg('库存不足');
        }

    });
}